<?php


namespace App\Http\Controllers\Admin;


use App\Models\Knowledge;
use App\Models\Lesson;
use App\Models\LessonSection;
use App\Models\LessonTest;

class LessonTestController extends Controller
{
    /*
     * @api get /course-api/a/lessons/{lessonId}/tests 测试列表
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param query int levelId 关卡ID
     * @param query int [pageSize] 页数，默认15
     * @param query int [page] 页码，默认1
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function index($lessonId)
    {
        $this->validate($this->request, [
            'levelId' => 'required|integer',
            'pageSize' => "integer|max:100|min:5",
            'page' => "integer|min:1",
        ]);
        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));
        $levelId = $this->request->input('levelId');
        $query = LessonTest::where('lesson_id', $lessonId)->where('level_id', $levelId);
        $total = $query->count();
        $attachments = $query->orderBy('sequence', 'asc')->orderBy('id', 'desc')->forPage($page, $pageSize)
            ->get(['id', 'name', 'description']);
        $items = [];
        if ($total) {
            foreach ($attachments as $attachment) {
                $items[] = [
                    'id' => $attachment->id,
                    'name' => $attachment->getName(),
                    'description' => $attachment->getDescription(),
                ];
            }
        }
        return $this->sendSuccessJson(['total'=>$total, 'items'=>$items]);
    }


    /*
     * @api get /course-api/a/lessons/{lessonId}/knowledge 课时知识点列表
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param query int [pageSize] 页数，默认15
     * @param query int [page] 页码，默认1
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function knowledge($lessonId)
    {
        $this->validate($this->request, [
            'pageSize' => "integer|max:100|min:5",
            'page' => "integer|min:1",
        ]);
        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));

        /** @var Lesson $query */
        $query = Lesson::find($lessonId)->knowledge();
        $total = $query->count();
        $knowledge = $query->forPage($page, $pageSize)
            ->get();
        $items = [];
        if ($total) {
            foreach ($knowledge as $knowledgeObj) {
                $items[] = [
                    'id' => $knowledgeObj->id,
                    'name' => $knowledgeObj->getName(),
                ];
            }
        }
        return $this->sendSuccessJson(['total'=>$total, 'items'=>$items]);
    }



    /*
     * @api post /course-api/a/lessons/{lessonId}/tests 新增测试
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param body string name 课时名称
     * @param body string description 课时描述
     * @param body int templateId 模版
     * @param body levelId templateId 模版
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function store($lessonId)
    {
        $this->request->merge(['lesson_id' => $lessonId]);
        $this->validate($this->request, [
            'lesson_id' => 'required|string',
            'name' => 'required|string',
            'description' => 'string',
            'templateId' => 'required|integer',
            'levelId' => 'required|integer'
        ]);
        $name = $this->request->input('name');
        $levelId = $this->request->input('levelId');
        $description = $this->request->input('description');
        $templateId = $this->request->input('templateId');

        /** @var LessonTest $lessonTestObj */
        $lessonTestObj =  new LessonTest();
        $lessonTestObj->setName($name);
        $lessonTestObj->setDescription($description);
        $lessonTestObj->setLessonId($lessonId);
        $lessonTestObj->setTemplateId($templateId);
        $lessonTestObj->setLevelId($levelId);
        $lessonTestObj->save();
        return $this->sendSuccessJson($lessonTestObj->id);
    }

    /*
     * @api put /course-api/a/lessons/{lessonId}/tests/{testId} 修改测试
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param path int testId 测试ID
     * @param body string name 课时名称
     * @param body string description 课时描述
     * @param body array content 课时描述
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function update($testId)
    {
        $this->request->merge(['id' => $testId]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'name' => 'string',
            'description' => 'string',
            'content' => 'array',
            'levelId' => 'integer'
        ]);
        /** @var LessonTest $lessonTestObj */
        $lessonTestObj = LessonTest::find($testId);
        if ($this->request->has('name')) {
            $lessonTestObj->setName($this->request->input('name'));
        }
        if ($this->request->has('description')) {
            $lessonTestObj->setDescription($this->request->input('description'));
        }
        if ($this->request->has('content')) {
            $lessonTestObj->setContent($this->request->input('content'));
        }
        if ($this->request->has('levelId')) {
            $lessonTestObj->setLevelId($this->request->input('levelId'));
        }
        $lessonTestObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api put /course-api/a/lessons/{lessonId}/tests-sequence 测试排序
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param path int testIds 课时页面ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function sequence($lessonId)
    {
        $this->request->merge(['lessonId'=>$lessonId]);
        $this->validate($this->request, [
            'lessonId' => 'required|integer',
            'testIds' => 'array',
        ]);
        $testIds = $this->request->input('testIds');
        foreach ($testIds as $index => $testId) {
            /** @var LessonTest $lessonTestObj */
            $lessonTestObj = LessonTest::find($testId);
            $lessonTestObj->setSequence($index);
            $lessonTestObj->save();
        }
        return $this->sendSuccessJson();
    }


    /*
     * @api get /course-api/a/lessons/{lessonId}/tests/{testId} 测试详情
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param path int testId 测试ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function show($lessonId, $testId)
    {
        /** @var LessonTest $lessonTestObj */
        $lessonTestObj = LessonTest::find($testId);

        $data = [
            'id' => $lessonTestObj->id,
            'name' => $lessonTestObj->getName(),
            'description' => $lessonTestObj->getDescription(),
            'content' => $lessonTestObj->getContent(),
            'templateId' => $lessonTestObj->getTemplateId()
        ];
        return $this->sendSuccessJson($data);
    }

    /*
     * @api delete /course-api/a/lessons/{lessonId}/tests/{testId} 删除测试
     * @version 0.0.1
     * @group admin
     *
     * @param path int lesson_id 课时ID
     * @param path int testId 测试ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function destroy($testId)
    {
        LessonTest::destroy(intval($testId));
        return $this->sendSuccessJson();
    }
}
